package flc.ast.dialog;

import static com.blankj.utilcode.util.StringUtils.getString;

import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.ToastUtils;

import flc.ast.R;
import flc.ast.databinding.DialogSetAnswerBinding;
import flc.ast.utils.Content;
import stark.common.basic.base.BaseSmartDialog;
import stark.common.basic.utils.SPUtil;

public class SetAnswerDialog extends BaseSmartDialog<DialogSetAnswerBinding> implements View.OnClickListener {

    public boolean isForget;
    private String[] answerArr;
    private int answerPos = 0;


    public SetAnswerDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int getGravity() {
        return Gravity.CENTER;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_set_answer;
    }

    @Override
    protected void initView(View view) {
        //密保设置
        mDataBinding.ivAnswerChange.setOnClickListener(this);
        mDataBinding.ivAnswerComp.setOnClickListener(this);
        //密保问题
        answerArr = new String[]{getString(R.string.answer_problem1), getString(R.string.answer_problem2), getString(R.string.answer_problem3), getString(R.string.answer_problem4)};
        mDataBinding.tvAnswerProblem.setText(answerArr[answerPos]);
        if (isForget) {
            mDataBinding.tvAnswerText.setText(R.string.input_answer);
            String spProblem = SPUtil.getString(getContext(), Content.myProblem, "");
            mDataBinding.tvAnswerProblem.setText(spProblem);
            mDataBinding.ivAnswerChange.setVisibility(View.GONE);
        } else {
            mDataBinding.tvAnswerText.setText(R.string.set_answer_tip2);
            mDataBinding.ivAnswerChange.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ivAnswerChange://切换密保问题
                QuestionDialog questionDialog = new QuestionDialog(getContext());
                questionDialog.setListener(new QuestionDialog.Ilistener() {
                    @Override
                    public void clickQuestion(int position) {
                        answerPos = position;
                        mDataBinding.tvAnswerProblem.setText(answerArr[answerPos]);
                    }
                });
                questionDialog.show();
                break;
            case R.id.ivAnswerComp://完成设置
                String answer = mDataBinding.etAnswerText.getText().toString();
                if (TextUtils.isEmpty(answer)) {
                    ToastUtils.showShort(getString(R.string.please_input_answer));
                    return;
                }
                if (isForget) {
                    //忘记密码
                    String spAnswer = SPUtil.getString(getContext(), Content.myAnswer, "");
                    if (answer.equals(spAnswer)) {
                        //进入设置密码布局
                        dismiss();
                        listener.setPassword(answerArr[answerPos], answer);
                    } else {
                        mDataBinding.etAnswerText.setText("");
                        ToastUtils.showShort(R.string.answer_input_error);
                    }
                } else {
                    listener.setPassword(answerArr[answerPos], answer);
                    dismiss();
                }
                break;
        }
    }


    private Listener listener;

    public void setListener(Listener listener) {
        this.listener = listener;
    }

    public interface Listener {
        void setPassword(String question, String answer);

    }
}
